package com.hnkywl.sts.service.jhgl;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.hnkywl.sts.dao.jhgl.BgfkjhDao;
import com.hnkywl.sts.entity.jhgl.Bgfkjh;
import com.hnkywl.sts.service.sys.YgService;
import com.hnkywl.sts.util.BaseUtils;
import com.hnkywl.sts.util.EasyuiAdapter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: ccr
 * Date: 13-6-29
 * Time: 上午11:25
 * To change this template use File | Settings | File Templates.
 */
@Service
public class BgfkjhService {
    private BgfkjhDao bgfkjhDao;

    public String bgfkjhList(Integer nf, Integer yf, Long gsId, Long gysId, Integer pageNo, Integer pageSize) {
        Map<String, Object> result = bgfkjhDao.findBgfkjh(nf, yf, gsId, gysId, pageNo, pageSize);
        Integer total = (Integer) result.get("total");
        List<Map> rows = (List<Map>) result.get("rows");
        List<Map> footer = (List<Map>) result.get("footer");
        Gson gson = new GsonBuilder().create();
        String rowsJson = gson.toJson(rows);
        String json = "{\"rows\":" + rowsJson + ",\"total\":" + total + "}";
        return json;
    }

    public Map bgfkjhListExport(Integer nf, Integer yf, Long gsId, Long gysId, Integer pageNo, Integer pageSize) {
        Map<String, Object> result = bgfkjhDao.findBgfkjh(nf, yf, gsId, gysId, pageNo, pageSize);
        return result;
    }

    public String bgfkjhList(Long gsId, Long gysId, Integer zftype) {
        List<Bgfkjh> bgfkjhList = bgfkjhDao.findBgfkjh(gsId, gysId, zftype);
        return "[" + BaseUtils.fetchCollectionToJson(".", false, bgfkjhList, "nbgs.qc", "gys.qc", "je", "zdr.xm", "zdrq", "bz") + "]";
    }

    /**
     * 货币支付保存
     *
     * @param bgfkjh
     */
    @Transactional
    public void saveHbzf(Bgfkjh bgfkjh) {
        Calendar c = Calendar.getInstance();
        c.setTime(bgfkjh.getZdrq());
        bgfkjh.setZdr(YgService.getCurrentYg());
        bgfkjh.setZftype(1);
        bgfkjh.setZjfkyear(c.get(Calendar.YEAR));
        bgfkjh.setZjkfmonth(c.get(Calendar.MONTH));
        bgfkjhDao.save(bgfkjh);
    }

    /**
     * 冲销保存
     *
     * @param bgfkjh
     */
    @Transactional
    public void saveCx(Bgfkjh bgfkjh) {
        Calendar c = Calendar.getInstance();
        bgfkjh.setZdrq(c.getTime());
        bgfkjh.setZdr(YgService.getCurrentYg());
        bgfkjh.setZftype(2);
        bgfkjh.setZjfkyear(c.get(Calendar.YEAR));
        bgfkjh.setZjkfmonth(c.get(Calendar.MONTH));
        bgfkjhDao.save(bgfkjh);
    }

    @Autowired
    public void setBgfkjhDao(BgfkjhDao bgfkjhDao) {
        this.bgfkjhDao = bgfkjhDao;
    }
}
